package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/myEmployee")
public class Employee01Controller {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("mylogin")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        String password = employee.getPassword();
        String username = employee.getUsername();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        QueryWrapper<Employee> qw = new QueryWrapper<>();
        LambdaQueryWrapper<Employee> eq = new LambdaQueryWrapper<>();
        eq.eq(Employee::getUsername,username);
        Employee result = employeeService.getOne(eq);
        if (result==null){
            return R.error("登录失败");
        }
        if (result.getPassword().equals(password)){
            return R.error("登录失败");
        }
        if (result.getStatus()==0){
            return R.error("账户已禁用");
        }
        session.setAttribute("employee",result.getId());
        return R.success(result);
    }
}
